package com.lihepeng.leecode.interview;

import com.lihepeng.leecode.linkedList.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class Solution02 {
    public static int[] retainAll(int[] array1, int[] array2) {
        List<Integer> list = new ArrayList<>();
        int p1 = 0, p2 = 0;
        while (p1 < array1.length && p2 < array2.length) {
            if (array1[p1] == array2[p2]) {
                if (!list.contains(array1[p1])) {
                    list.add(array1[p1]);
                }
                p1++;
                p2++;
            } else if (array1[p1] < array2[p2]) {
                p1++;
            } else {
                p2++;
            }

        }
        return list.stream().mapToInt(Integer::valueOf).toArray();
    }

    public static char find(char[] char1, char[] chars2) {
        if (char1 == null || chars2 == null) {
            return ' ';
        }
        int count1 = char1.length;
        int count2 = chars2.length;
        int mincount = count1 < count2 ? count1 : count2;
        for (int i = 0; i < mincount; i++) {
            if (char1[i] != chars2[i]) {
                return char1[i];
            }
        }
        // 都一样返回最长的那个的最后一位
        return count1 > count2 ? char1[count1 - 1] : chars2[count2 - 1];
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.value : 0;
            int n2 = l2 != null ? l2.value : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }

    public static void main(String[] args) {
        char[] char1 = {'a', 'b', 'c', 'd'};
        char[] char2 = {'a', 'b', 'c'};
        char c = find(char1, char2);
        System.out.println(c);
    }
}
